﻿using Biomedica.Graphics;
using KinectWindows.Math;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Biomedica.DSP
{
    public class Buffer : ICollection<Vector2>
    {
        public Queue<Vector2> buffer { set; get; }
        public int Size { set; get; }
        public Buffer()
        {
            Size = 10;
            buffer = new Queue<Vector2>();
        }
        public Buffer(int size)
        {
            Size = size;
            buffer = new Queue<Vector2>();
        }
        public Buffer(Vector2[] data)
        {
            buffer = new Queue<Vector2>();
            for (int i = 0; i < data.Length; i++)
                buffer.Enqueue(data[i]);

            Size = data.Length;
        }

       
        public void Push(Vector2 data)
        {
            buffer.Enqueue(data);
            if (buffer.Count > Size) buffer.Dequeue();
        }
        public Vector2 GetFirstElement()
        {
            return buffer.ToArray()[0];
        }
        public Vector2 getElementAt(int indx)
        {
            return buffer.ToArray()[0];
        }


        public void Add(Vector2 item)
        {
            buffer.Enqueue(item);
            if (buffer.Count > Size) buffer.Dequeue();
        }

        public void Clear()
        {
            buffer.Clear();
        }

        public bool Contains(Vector2 item)
        {
            return buffer.Contains(item);
        }

        public void CopyTo(Vector2[] array, int arrayIndex)
        {
            buffer.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return buffer.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(Vector2 item)
        {
            List<Vector2> f=new List<Vector2>(buffer.ToArray());
            bool b=f.Remove(item);
            if (b )buffer = new Queue<Vector2>(f);
            return b;
        }

        public IEnumerator<Vector2> GetEnumerator()
        {
            List<Vector2> d=new List<Vector2>(buffer.ToArray());
            return d.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return buffer.GetEnumerator();
        }

        public Vector2[] ToArray()
        {
            return buffer.ToArray();
        }

        internal Vector2 Mean()
        {
            Vector2[] vC=this.ToArray();
            Vector2 meanV = Vector2.Zero;
            for (int i = 0; i < vC.Length; i++)
            {
                meanV.X += vC[i].X;
                meanV.Y += vC[i].Y;
            }
            return meanV/Size;
        }
    }
}
